<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<rapid:override name="frame-header-style">
	<style>
		/*覆盖 layui*/
		.layui-input-block {
			margin: 0px 10px;
		}
		
		.layui-table {
			margin-top: 0;
		}
		
		.layui-col-md4 {
			padding: 10px;
		}
		
		.layui-col-md8 {
			padding: 10px;
		}
		
		.layui-btn {
			margin: 2px 0 !important;
		}
</style>
</rapid:override>
<rapid:override name="frame-header-script">
	<script src="resources/js/jquery.min.js"></script>
	<script>

	$(function(){
	});
		/* 添加分类审查用户名是否重复 */
		function add(){
			var flag = true;
			var categoryName = $("#categoryName").val();
			if(categoryName!=""){
				$.ajax({
					url:"category/categoryName",
					type:"post",
					data:{categoryName:categoryName},
					async:false,
					cache:false,
					success:function(data){
						if(data=="1"){
							alert("分类名重复");
							flag = false;
						}
					}
				});
			}
			
			 if(flag==true){
				document.getElementById("myForm").submit();
			} 
		}
		function update(){
			var flag = true;
			var categoryName = $("#categoryName").val();
			if(categoryName!='${cate.categoryName}'){
				if(categoryName!=""){
					$.ajax({
						url:"category/categoryName",
						type:"post",
						data:{categoryName:categoryName},
						async:false,
						cache:false,
						success:function(data){
							if(data=="1"){
								alert("用户名重复");
								flag = false;
							}
						}
					});
				}
			}
				if(flag==true){
					document.getElementById("myForm").submit();
				} 
		}
</script>
</rapid:override>
<rapid:override name="frame-header-script">
	<script src="resources/js/jquery.min.js"></script>
</rapid:override>
<rapid:override name="frame-content">
	<div class="layui-col-md4">
		<c:if test="${flag=='edit' }">
			
			<form class="layui-form" method="post" id="myForm"
				action="category/updateSubmit">
				<input type="hidden" name="categoryId" value="${cate.categoryId }">
				<div class="layui-form-item">
					<div class="layui-input-block">
						<strong>添加分类</strong>
					</div>
					<div class="layui-input-block">
						名称 <span style="color: #FF5722;">*</span> <input type="text" id="categoryName"
							name="categoryName" placeholder="请输入分类名称" autocomplete="off" value="${cate.categoryName }"
							class="layui-input" required>
					</div>
					<br>
					<div class="layui-input-block">
						父节点 <span style="color: #FF5722;">*</span> <select
							name="categoryPid" class="layui-input" required>
							<option value="0" <c:if test="${cate.categoryPid==0 }">selected</c:if>>无</option>
							<c:forEach var="big" items="${bigCategory }">
								
								<option value="${big.categoryId }" <c:if test="${cate.categoryPid==big.categoryId }">selected</c:if> >${big.categoryName }</option>
							</c:forEach>
						</select>
					</div>
					<br>
					<div class="layui-input-block">
						分类描述 <input type="text" name="categoryDescription" value="${cate.categoryDescription }"
							placeholder="请输入分类描述" autocomplete="off" class="layui-input">
					</div>
					<br>
					<div class="layui-input-block">
						图标样式 <input type="text" name="categoryIcon" value="${cate.categoryIcon }"
							placeholder="请输入图标样式,如 fa fa-coffee" autocomplete="off"
							class="layui-input">
					</div>
					<br>
					<div class="layui-input-block">
						<button class="layui-btn" lay-filter="formDemo" type="button" onclick="return update()">修改</button>
						<a href="category/list" class="layui-btn" lay-filter="formDemo" >去添加分类</a>
					</div>
				</div>
			</form>
		</c:if>
		<c:if test="${flag!='edit' }">
			<form class="layui-form" method="post" id="myForm"
				action="category/insertSubmit">
				<div class="layui-form-item">
					<div class="layui-input-block">
						<strong>添加分类</strong>
					</div>
					<div class="layui-input-block">
						名称 <span style="color: #FF5722;">*</span> <input type="text" id="categoryName"
							name="categoryName" placeholder="请输入分类名称" autocomplete="off" value="${cate.categoryName }"
							class="layui-input" required>
					</div>
					<br>
					<div class="layui-input-block">
						父节点 <span style="color: #FF5722;">*</span> <select
							name="categoryPid" class="layui-input" required>
							<option value="0" <c:if test="${cate.categoryPid==0 }">selected</c:if>>无</option>
							<c:forEach var="big" items="${bigCategory }">
								
								<option value="${big.categoryId }"  <c:if test="${cate.categoryPid==big.categoryId }">selected</c:if> >${big.categoryName }</option>
							</c:forEach>
						</select>
					</div>
					<br>
					<div class="layui-input-block">
						分类描述 <input type="text" name="categoryDescription" value="${cate.categoryDescription }"
							placeholder="请输入分类描述" autocomplete="off" class="layui-input">
					</div>
					<br>
					<div class="layui-input-block">
						图标样式 <input type="text" name="categoryIcon" value="${cate.categoryIcon }"
							placeholder="请输入图标样式,如 fa fa-coffee" autocomplete="off"
							class="layui-input">
					</div>
					<br>
					<div class="layui-input-block">
						<button class="layui-btn" lay-filter="formDemo" type="button" onclick="return add()">添加</button>
					</div>
				</div>
			</form>
		</c:if>
		
	</div>
	<div class="layui-col-md8">
		<table class="layui-table">
			<colgroup>
				<col width="300">
				<col width="100">
				<col width="100">
				<col width="100">
				<col width="50">
				<col width="50">
			</colgroup>
			<thead>
				<tr>
					<th>名称</th>
					<th>文章数</th>
					<th>操作</th>
					<th>ID</th>
					<th>pid</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach var="big" items="${bigCategory }">
					<tr>
						<td>
							<a href="/category/1" target="_blank">
								${big.categoryName }
							</a>
						</td>
						
						<td><a href="/category/${big.categoryId }" target="_blank">${big.articleCount }</a></td>
						
						<td><a href="category/edit/${big.categoryId }"
							class="layui-btn layui-btn-mini">编辑</a> 
							<c:if test="${big.articleCount==0 }">
								<a href="category/delete/${big.categoryId }"
								class="layui-btn layui-btn-danger layui-btn-mini"
								onclick="return confirm('确认要删除吗')">删除</a>
							</c:if>
						</td>
						
						<td>${big.categoryId }</td>
						<td class="cate-parent">${big.categoryPid}</td>
					</tr>
					<c:forEach var="c" items="${category }">
						<c:if test="${c.categoryPid==big.categoryId }">
							<tr>
								<td>
									<a href="/category/1" target="_blank">
										——${c.categoryName } 
									</a>
								</td>
								<td><a href="/category/${c.categoryId }" target="_blank">${c.articleCount }</a></td>
								<td><a href="category/edit/${c.categoryId }"
									class="layui-btn layui-btn-mini">编辑</a> 
									<c:if test="${c.articleCount==0 }">
										<a href="/admin/category/delete/${c.categoryId }"
										class="layui-btn layui-btn-danger layui-btn-mini"
										onclick="return confirmDelete()">删除</a>
									</c:if>
								</td>
								<td>${c.categoryId }</td>
								<td class="cate-parent">${c.categoryPid}</td>
							</tr>
						</c:if>
					</c:forEach>
				</c:forEach>
			
				
			</tbody>
		</table>
		<blockquote class="layui-elem-quote layui-quote-nm">
			温馨提示：
			<ul>
				<li>分类最多只有两级，一级分类pid=0，二级分类pid=其父节点id</li>
				<li>如果该分类包含文章，将不可删除</li>
			</ul>
		</blockquote>
	</div>

</rapid:override>
<%@ include file="../framework.jsp"%>